package util;


import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class GmailUtilities {
    final String miCorreo = "alumni.itba@gmail.com";
    final String miContrasena = "itba1234";
    final String servidorSMTP = "smtp.gmail.com";
    final String puertoEnvio = "465";
    String mailReceptor = null;
    String asunto = null;
    String cuerpo = null;

    public GmailUtilities(String mailReceptor, MailTitleBody mtb) {
        this.mailReceptor = mailReceptor;
        this.asunto = mtb.getTitle();
        this.cuerpo = mtb.getBody();

        Properties props = new Properties();
        props.put("mail.smtp.user", miCorreo);
        props.put("mail.smtp.host", servidorSMTP);
        props.put("mail.smtp.port", puertoEnvio);
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.socketFactory.port", puertoEnvio);
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");

        SecurityManager security = System.getSecurityManager();

        try {
            Authenticator auth = new AutentificadorSMTP();
            Session session = Session.getInstance(props, auth);
            // session.setDebug(true);

            MimeMessage msg = new MimeMessage(session);
            msg.setText(cuerpo);
            msg.setSubject(asunto);
            msg.setFrom(new InternetAddress(miCorreo));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
                    mailReceptor));
            Transport.send(msg);
        } catch (Exception mex) {
            mex.printStackTrace();
        }

    }

    private class AutentificadorSMTP extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(miCorreo, miContrasena);
        }
    }

}

//package util;
//
//import java.util.Properties;
//
//import javax.mail.Message;
//import javax.mail.Session;
//import javax.mail.Transport;
//import javax.mail.internet.InternetAddress;
//import javax.mail.internet.MimeMessage;
//
//public class GmailUtilities {
//	private static String smtpHostName="smtp.alu.itba.edu.ar";
//	private static String userSend;
//	private static String toSend;
//	
//	public GmailUtilities(String user, String to){
//		userSend=user;
//		toSend=to;
//	}
//	
//	
//	public  void connectSend(String mensaje) throws Exception {
////		boolean debug = true;
//		Properties props = new Properties();
//		props.put("mail.smtp.host", smtpHostName);
//
//		Session session = Session.getDefaultInstance(props,
//				null);
////		session.setDebug(debug);
//
//		Message msg = new MimeMessage(session);
//
//		InternetAddress addressFrom = new InternetAddress(userSend);
//
//		msg.setFrom(addressFrom);
//		InternetAddress addressTo = new InternetAddress(toSend);
//		msg.setRecipient(Message.RecipientType.TO, addressTo);
//
//		msg.setSubject("ERROR FATAL");
//		msg.setContent(mensaje, "text/plain");
//		msg.setSentDate(new java.sql.Date(new java.util.Date().getTime()));
//		Transport.send(msg);
//	}
//}
